package com.huawei.mpchartexample;

import com.huawei.mpchartlib.charts.RadarChart;
import com.huawei.mpchartlib.components.AxisBase;
import com.huawei.mpchartlib.components.Legend;
import com.huawei.mpchartlib.components.XAxis;
import com.huawei.mpchartlib.components.YAxis;
import com.huawei.mpchartlib.data.RadarData;
import com.huawei.mpchartlib.data.RadarDataSet;
import com.huawei.mpchartlib.data.RadarEntry;
import com.huawei.mpchartlib.formatter.IAxisValueFormatter;
import com.huawei.mpchartlib.interfaces.datasets.IRadarDataSet;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;

import java.util.ArrayList;

public class RadarChartAbility extends Ability {

    private RadarChart chart;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_radarchart);

        chart = (RadarChart) findComponentById(ResourceTable.Id_radarChart);
        {   // // Chart Style // //

            // background color
            ShapeElement background= new ShapeElement();
            background.setShape(ShapeElement.RECTANGLE);
            background.setRgbColor(new RgbColor(60,65,82));
            chart.setBackground(background);

            // disable description text
            chart.getDescription().setEnabled(false);

            // set web line style
            chart.setWebLineWidth(1.f);
            chart.setWebColor(Color.GRAY.getValue());
            chart.setWebLineWidthInner(1.f);
            chart.setWebColorInner(Color.GRAY.getValue());
            chart.setWebAlpha(180);
        }

        XAxis xAxis;
        {   // // X-Axis Style // //
            xAxis = chart.getXAxis();
            xAxis.setFont(Font.DEFAULT);
            xAxis.setTextSize(9f);
            xAxis.setYOffset(0f);
            xAxis.setXOffset(0f);

            xAxis.setValueFormatter(new IAxisValueFormatter() {

                private final String[] mActivities = new String[]{"Burger", "Steak", "Salad", "Pasta", "Pizza"};

                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    return mActivities[(int) value % mActivities.length];
                }
            });
            xAxis.setTextColor(Color.WHITE.getValue());
        }

        YAxis yAxis;
        {   // // X-Axis Style // //
            yAxis = chart.getYAxis();
            yAxis.setFont(Font.DEFAULT);
            yAxis.setLabelCount(5, false);
            yAxis.setTextSize(9f);
            yAxis.setAxisMinimum(0f);
            yAxis.setAxisMaximum(80f);
            yAxis.setDrawLabels(false);
        }

        // add data
        setData();
        chart.invalidate();

        {   // // Legend // //
            // get the legend (only possible after setting data)
            Legend l = chart.getLegend();

            l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
            l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
            l.setDrawInside(false);
            l.setFont(Font.DEFAULT);
            l.setXEntrySpace(7f);
            l.setYEntrySpace(5f);
            l.setTextColor(Color.WHITE.getValue());
        }

        // draw chart
        Component.DrawTask drawTask = new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                chart.onDraw(canvas);
            }
        };
        chart.addDrawTask(drawTask);
    }

    private void setData() {

        float mul = 80;
        float min = 20;
        int cnt = 5;

        ArrayList<RadarEntry> entries1 = new ArrayList<>();
        entries1.add(new RadarEntry(50));
        entries1.add(new RadarEntry(30));
        entries1.add(new RadarEntry(40));
        ArrayList<RadarEntry> entries2 = new ArrayList<>();
        entries2.add(new RadarEntry(10));
        entries2.add(new RadarEntry(35));
        entries2.add(new RadarEntry(25));

        // NOTE: The order of the entries when being added to the entries array determines their position around the center of
        // the chart.
        for (int i = 0; i < cnt; i++) {
            float val1 = (float) (Math.random() * mul) + min;
            entries1.add(new RadarEntry(val1));

            float val2 = (float) (Math.random() * mul) + min;
            entries2.add(new RadarEntry(val2));
        }

        RadarDataSet set1 = new RadarDataSet(entries1, "Last Week");
        set1.setColor(Color.rgb(103, 110, 129));
        set1.setFillColor(Color.rgb(103, 110, 129));
        set1.setDrawFilled(true);
        set1.setFillAlpha(180);
        set1.setLineWidth(2f);
        set1.setDrawHighlightCircleEnabled(true);
        set1.setDrawHighlightIndicators(false);

        RadarDataSet set2 = new RadarDataSet(entries2, "This Week");
        set2.setColor(Color.rgb(121, 162, 175));
        set2.setFillColor(Color.rgb(121, 162, 175));
        set2.setDrawFilled(true);
        set2.setFillAlpha(180);
        set2.setLineWidth(2f);
        set2.setDrawHighlightCircleEnabled(true);
        set2.setDrawHighlightIndicators(false);

        ArrayList<IRadarDataSet> sets = new ArrayList<>();
        sets.add(set1);
        sets.add(set2);

        RadarData data = new RadarData(sets);
        data.setValueFont(Font.DEFAULT);
        data.setValueTextSize(8f);
        data.setDrawValues(false);
        data.setValueTextColor(Color.WHITE.getValue());

        chart.setData(data);
    }
}

